package com.fudan.backend.service.impl;

import com.fudan.backend.dao.CommodityDao;
import com.fudan.backend.dao.CommodityLaunchRequestDao;
import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Commodity;
import com.fudan.backend.pojo.CommodityLaunchRequest;
import com.fudan.backend.pojo.statics.RequestStatus;
import com.fudan.backend.service.CommodityLaunchRequestService;
import com.fudan.backend.util.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
public class CommodityLaunchRequestServiceImpl implements CommodityLaunchRequestService {
    @Autowired
    CommodityLaunchRequestDao commodityLaunchRequestDao;
    @Autowired
    Snowflake snowflake;
    @Autowired
    CommodityDao commodityDao;
    @Override
    public PageInfo<CommodityLaunchRequest> getAllCommodityLaunchRequestByPage(Integer current, Integer pagesize) {
        PageHelper.startPage(current, pagesize);
        List<CommodityLaunchRequest> list = commodityLaunchRequestDao.getAllCommodityLaunchRequest();
        return new PageInfo<CommodityLaunchRequest>(list);
    }

    @Override
    public PageInfo<CommodityLaunchRequest> getStoreCommodityLaunchRequestByPage(String storeId, Integer current, Integer pagesize) {
        PageHelper.startPage(current, pagesize);
        List<CommodityLaunchRequest> list = commodityLaunchRequestDao.getCommodityLaunchRequestByStoreId(storeId);
        return new PageInfo<CommodityLaunchRequest>(list);
    }

    @Override
    public CommodityLaunchRequest getCommodityLaunchRequestByCommodityLaunchRequestId(String id) {
        CommodityLaunchRequest c = commodityLaunchRequestDao.getCommodityLaunchRequestByCommodityLaunchRequestId(id);
        if(c == null){
            throw new BaseException(RetCode.COMMODITY_LAUNCH_REQUEST_NOT_FOUND);
        }
        return c;
    }

    @Override
    public RetCode setCommodityLaunchRequestStatus(String commodityLaunchRequestId, Integer status) {
        try {
            commodityLaunchRequestDao.setCommodityLaunchRequestStatus(commodityLaunchRequestId, status);
        }
        catch (Exception e){
            throw new BaseException(RetCode.COMMODITY_LAUNCH_FAILURE);
        }

        return RetCode.COMMODITY_SUCCESS;
    }

    @Override
    public RetCode setInfo(String commodityLaunchRequestId, String info) {
        try{
            commodityLaunchRequestDao.setCommodityLaunchRequestInfo(commodityLaunchRequestId, info);
        }
        catch(Exception e){
            throw new BaseException(RetCode.COMMODITY_LAUNCH_FAILURE);
        }

        return RetCode.COMMODITY_SUCCESS;
    }

    @Override
    public RetCode addCommodityLaunchRequest(CommodityLaunchRequest request) {
        try{
            Date date = new Date();
            request.setInitiateTime(date);
            request.setProcessTime(null);
            request.setStatus(0);
            request.setCommodityLaunchRequestId(snowflake.nextId().toString());
            commodityLaunchRequestDao.insertCommodityLaunchRequest(request);
        }
        catch(Exception e){
            throw new BaseException(RetCode.COMMODITY_LAUNCH_FAILURE);
        }
        return RetCode.COMMODITY_SUCCESS;
    }

    @Override
    @Transactional
    public RetCode process(CommodityLaunchRequest request) {
        try{
            Date date = new Date();
            request.setProcessTime(date);
            commodityLaunchRequestDao.setStatusInfoProcessTime(request.getCommodityLaunchRequestId(), request.getStatus(), request.getInfo(), request.getProcessTime());
            if(request.getStatus().equals(RequestStatus.APPROVED)){
                Commodity commodity = new Commodity(
                        snowflake.nextId().toString(),
                        request.getStoreId(),
                        request.getCommodityName(),
                        request.getCategoryId(),
                        request.getPrice(),
                        request.getStocks(),
                        request.getPictures(),
                        request.getDescription()
                );
                commodityDao.insertCommodity(commodity);
            }
        }
        catch (Exception e){
            throw new BaseException(RetCode.COMMODITY_LAUNCH_FAILURE);
        }
        return RetCode.COMMODITY_SUCCESS;
    }

}
